#! /bin/sh

source /etc/rc.d/common

VTL_USER="vtl"
MHVTL_CONFIG_PATH="/etc/mhvtl"

check_args ${@}

start() {
    /bin/echo "Starting mhVTL services..."
    # Make sure the mhVTL user can read/write
    /bin/chown -R ${VTL_USER} ${MHVTL_CONFIG_PATH}
    # Load the mhVTL module
    /sbin/modprobe mhvtl || exit 1
    # Build the library media
    /usr/bin/make_vtl_media ${VTL_USER}
    # Build the library configuration
    /usr/bin/build_library_config ${VTL_USER}
}

stop() {
    /bin/echo "Stopping mhVTL services..."
    # Close drive tapes
    for i in `ps -eo args | awk '/^vtltape -q/ {print $3}'`; do
        /usr/bin/vtlcmd ${i} exit
        /bin/usleep 100
    done
    # Close libraries
    for i in `ps -eo args | awk '/^vtllibrary -q/ {print $3}'`; do
        /usr/bin/vtlcmd ${i} exit
        /bin/usleep 100
    done
}

status() {
    if /bin/grep mhvtl /proc/modules > /dev/null 2>&1; then
        exit ${RUNNING}
    else
        exit ${STOPPED}
    fi
}

# Perform specified action
${1}
